<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="12" >
        <div style="margin-left: 380px">
          <el-statistic
            :value="totalStatics.totalCount"
            :title="totalTitle"
          >
            <template slot="prefix">
              <i class="el-icon-user-solid" style="color: #53ef92"></i>
            </template>
          </el-statistic>
        </div>
      </el-col>
      <el-col :span="12" >
        <div style="margin-right: 450px">
          <el-statistic title="男女比">
            <template slot="formatter">
              {{ this.totalStatics.scale }}
            </template>
          </el-statistic>
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="elderlyapartmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公寓名称" align="center" prop="apartmentName" />
      <el-table-column label="可居住总人数" align="center" prop="capacity" />
      <el-table-column label="公寓居住率" align="center" prop="percent" >
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="20" :percentage= "scope.row.percent" status="success"></el-progress>
        </template>
      </el-table-column>
      <el-table-column label="公寓已住人数" align="center" prop="occupied" />
      <el-table-column label="负责人电话" align="center" prop="phoneNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['business:elderlyapartment:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改公寓信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body center >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="公寓名称" prop="apartmentName">
          <el-input v-model="form.apartmentName" placeholder="" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="可入住总人数" prop="capacity">
          <el-input
            placeholder="请输入内容"
            v-model="form.capacity"
            :disabled="isDisabledCapacity" style="width: 300px">
          </el-input>
<!--          <el-input v-model="form.capacity" placeholder="" :disabled="true" style="width: 300px"/>-->
        </el-form-item>
        <el-form-item label="已住人数" prop="occupied">
          <el-input
            placeholder="请输入内容"
            v-model="form.occupied"
            :disabled="isDisabledOccupied" style="width: 300px">
          </el-input>
<!--          <el-input v-model="form.occupied" placeholder="" :disabled="true" style="width: 300px"/>-->
        </el-form-item>
        <el-form-item label="负责人电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="" style="width: 300px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer"  >
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listElderlyapartment, getElderlyapartment, delElderlyapartment, addElderlyapartment, updateElderlyapartment, getElderlyStatistics} from "@/api/business/elderlyapartment";

export default {
  name: "Elderlyapartment",
  data() {
    return {
      like: true,
      totalStatics:{},
      totalTitle: "总人数",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公寓信息表格数据
      elderlyapartmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apartmentName: null,
        capacity: null,
        occupied: null,
        phoneNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getStatistics();
  },
  computed: {
    isDisabledCapacity() {
      return this.form.capacity; // 如果input有值，则禁用，否则不禁用
    },
    isDisabledOccupied(){
      return this.form.occupied;
    }
  },
  methods: {
    /** 获取统计数据 **/
    getStatistics(){
      getElderlyStatistics().then(response =>{
        this.totalStatics = response.data;
        this.totalStatics.scale = this.totalStatics.maleCount  +"/"+ this.totalStatics.femaleCount;
        console.log(this.totalStatics)
      })
    },

    /** 查询公寓信息列表 */
    getList() {
      this.loading = true;
      listElderlyapartment(this.queryParams).then(response => {
        this.elderlyapartmentList = response.rows;
        this.elderlyapartmentList.forEach(item =>{
          item.percent = (item.occupied ? (item.occupied *100): 0) / item.capacity
        })
        console.log(this.elderlyapartmentList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        apartmentId: null,
        apartmentName: null,
        capacity: null,
        occupied: null,
        phoneNumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.apartmentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公寓信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const apartmentId = row.apartmentId || this.ids
      getElderlyapartment(apartmentId).then(response => {
        this.form = response.data;
        console.log(this.form);
        this.open = true;
        this.title = "修改公寓信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.apartmentId != null) {
            updateElderlyapartment(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElderlyapartment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const apartmentIds = row.apartmentId || this.ids;
      this.$modal.confirm('是否确认删除公寓信息编号为"' + apartmentIds + '"的数据项？').then(function() {
        return delElderlyapartment(apartmentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/elderlyapartment/export', {
        ...this.queryParams
      }, `elderlyapartment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style >
  .app-container {
    background: #d9ebec;
  }
</style>
